"""
title:后台广告列表page
author:何宗香
date：2022/1/9
"""



from Common.Base import Base, open_browser
from Page.AdminLoginPage import AdminLoginPage, login_url
from time import sleep

class AdvertisingManagement(Base):
    """封装表现层:制作定位器"""
    index = int()
    advertising_management_loc = ('xpath', '//ul[@id="menu-ul"]/li[5]')
    adverstinglist_loc = ('xpath', '//*[@id="sub-menu-ad_list"]/a')
    add_ad_loc = ('xpath', '/html/body/h1/a')
    ad_name_loc = ('xpath', '//*[@id="general-table"]/tbody[1]/tr[1]/td[2]/input')
    media_types_loc = ('css selector', '#general-table > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > select')
    file_loc = ('xpath', '//*[@id="0"]/tr[2]/td[2]/input')
    open_loc = ('xpath', '//*[@id="general-table"]/tbody[3]/tr[1]/td[2]/input[@value=1]')
    close_loc = ('xpath', '//*[@id="general-table"]/tbody[6]/tr[1]/td[2]/input[2]')
    close_loc1 = ('xpath', '//*[@id="general-table"]/tbody[3]/tr[1]/td[2]/input[2]')
    confirm_loc = ('xpath', '//*[@id="general-table"]/tbody[6]/tr[5]/td[2]/input[1]')
    confirm_loc1 = ('css selector', '#general-table > tbody:nth-child(3) > tr:nth-child(5) > td:nth-child(2) > input:nth-child(1)')



    result_adname_loc = ('xpath', '//*[@id="listDiv"]/table/tbody/tr[2]/td[1]/span')

    num_loc = ('css selector', '#totalRecords')
    delete_num_loc = ('css selector', '#totalRecords')

    def join_menu_frame(self):
        self.driver.switch_to.frame("menu-frame")

    def quit_menu_frame(self):
        self.driver.switch_to.parent_frame()

    def join_main_frame(self):
        self.driver.switch_to.frame("main-frame")

    def quit_main_frame(self):
        self.driver.switch_to.parent_frame()

    def ad_locator(self, index, value):
        if value == True:
            update_loc = (
            'css selector', f'#listDiv > table > tbody > tr:nth-child({index}) > td:nth-child(8) > span > a:nth-child(2)')
            return update_loc
        else:
            delete_loc = (
            'css selector', f'#listDiv > table > tbody > tr:nth-child({index}) > td:nth-child(8) > span > a:nth-child(3)')
            return delete_loc




    def click_advertising_management(self,):
        """点击广告管理"""
        self.click(self.advertising_management_loc)


    def click_adverstinglist(self):
        """点击广告列表"""
        self.click(self.adverstinglist_loc)

    def click_add_ad(self):
        """点击添加广告"""
        self.click(self.add_ad_loc)

    def click_update_ad(self, index=1, vaule=True):
        """点击编辑广告"""
        index += 1
        self.click(self.ad_locator(index, vaule))

    def click_delete_ad(self, index=1, value=False):
        """点击删除广告"""
        index += 1
        self.click(self.ad_locator(index, value))

    def confirm(self, option):
        """确认是否删除"""
        if option == '确定':
            self.switch_alert('accept')
        elif option == '取消':
            self.switch_alert('quit')

    def input_ad_name(self, ad_name):
        """输入广告名称"""
        self.send_keys(self.ad_name_loc, ad_name)


    def input_media_types(self, media_types):
        """输入媒介类型"""
        self.drop_down_list(self.media_types_loc, media_types)

    def upload_file(self, file):
        """上传文件"""
        self.send_keys(self.file_loc, file)


    def open_ad(self):
        """打开广告"""
        self.click(self.open_loc)

    def close_ad(self):
        """关闭广告"""
        self.click(self.close_loc)

    def click_confirm(self):
        """点击确定"""
        self.click(self.confirm_loc)


    def click_confirm1(self):
        """点击确定"""
        self.click(self.confirm_loc1)

    def is_add_sucess(self):
        """判断是否添加成功"""
        return self.get_element_text(self.num_loc)



    def is_delete_sucess(self):
        """判断是否删除成功"""
        return self.get_element_text(self.delete_num_loc)

    def is_upadate_sucess(self):
        """判断是否修改成功"""
        result2 = self.get_element_text(self.result_adname_loc)
        return result2

    def gain_value(self, loc):
        """获取元素value值"""
        return self.find_element(loc).get_attribute('value')


if __name__ == '__main__':
    driver = open_browser()
    admin_login = AdminLoginPage(driver)
    admin_login.go_url(login_url)
    ad = AdvertisingManagement(driver)
    ad.click_advertising_management()
    ad.click_adverstinglist()
    # ad.click_add_ad()
    ad.click_update_ad(1)

    ad.input_ad_name('首页广告111')

    # ad.input_media_types('图片')
    ad.upload_file(r'E:\软件测试\测试作业\ecshop项目\ECShop\ec--shop-group-5\Data\img.png')
    ad.close_ad()
    ad.click_confirm()

    # ad.click_delete_ad()
    # ad.confirm('确定')